package com.ruoyi.system.controller;

import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.PARTLINK;
import com.ruoyi.system.service.IPARTLINKService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * 大数据测试 提供者
 *
 * @author ruoyi
 * @date 2020-07-02
 */
@Slf4j
@RestController
@RequestMapping("pARTLINK")
public class PARTLINKController extends BaseController
{

	@Autowired
	private IPARTLINKService pARTLINKService;

	/**
	 * 查询${tableComment}
	 */
	@GetMapping("get/{oid}")
	public PARTLINK get(@PathVariable("oid") Double oid)
	{
		return pARTLINKService.selectPARTLINKById(oid);

	}

	/**
	 * 查询大数据测试列表
	 */
	@GetMapping("list")
	public R list(PARTLINK pARTLINK)
	{
		startPage();
        return result(pARTLINKService.selectPARTLINKList(pARTLINK));
	}


	/**
	 * 新增保存大数据测试
	 */
	@PostMapping("save")
	public R addSave(@RequestBody PARTLINK pARTLINK)
	{
		return toAjax(pARTLINKService.insertPARTLINK(pARTLINK));
	}

	/**
	 * 修改保存大数据测试
	 */
	@PostMapping("update")
	public R editSave(@RequestBody PARTLINK pARTLINK)
	{
		return toAjax(pARTLINKService.updatePARTLINK(pARTLINK));
	}

	/**
	 * 删除${tableComment}
	 */
	@PostMapping("remove")
	public R remove(String ids)
	{
		return toAjax(pARTLINKService.deletePARTLINKByIds(ids));
	}


	@GetMapping("/exports")
	@ResponseBody
	@ApiOperation(value = "get export",notes = "test")
	public void exports(PARTLINK partlink, HttpServletResponse response, HttpServletRequest request) {
//        if (regulationCountry.getDeptid() == null || regulationCountry.getDeptid() == "") {
//            regulationCountry.setDeptid("101");
//        }
//        Class<?> clazz = domain.getClass();
//        clazz.getDeclaredMethod(clazz.getName())

		try {
			String filename = UUID.randomUUID().toString() + "_" + ".xlsx";
			response.setCharacterEncoding("utf-8");
			response.setContentType("multipart/form-data");
			response.setHeader("Content-Disposition",
					"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, filename));
			List<PARTLINK> partlinks = pARTLINKService.selectPARTLINKList(partlink);
			ExcelUtil<PARTLINK> util = new ExcelUtil<PARTLINK>(PARTLINK.class);
			util.exportExcel(partlinks, "country",response.getOutputStream());
		} catch (Exception e) {
			log.error("下载文件失败", e);
		}
	}

	@PostMapping("/importData")
	@ResponseBody
	@ApiOperation(value = "post import",notes = "test")
	public String importData(MultipartFile file, boolean updateSupport) throws Exception
	{
		ExcelUtil<PARTLINK> util = new ExcelUtil<>(PARTLINK.class);
		List<PARTLINK> partlinks = util.importExcel(file.getInputStream());
		String message = pARTLINKService.importFile(partlinks, updateSupport);
		return message;
	}

	@GetMapping("/importTemplate")
	@ResponseBody
	@ApiOperation(value = "get importtemple",notes = "test")
	public void importTemplate(HttpServletResponse response, HttpServletRequest request)
	{
		try {
			String filename = "用户导入模板.xlsx";
			response.setCharacterEncoding("utf-8");
			response.setContentType("multipart/form-data");
			response.setHeader("Content-Disposition",
					"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, filename));
			ExcelUtil<PARTLINK> util = new ExcelUtil<>(PARTLINK.class);
			util.importTemplateExcel("大数据测试对象",response.getOutputStream());
		} catch (Exception e) {
			log.error("下载文件失败", e);
		}
	}

}
